<?php
namespace jy_mzhan;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		$ismid=$this->get('member',array('mid'=>$member['mid']),array('mid'));
		if($ismid){
			exi('该会员已经是绑定社区站，请勿重复绑定！','error');
		}
		$this->insert('member',$member);
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('member',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
		}else{
			$update['remark']=$remark;
		}
		$this->update('member',$update,array('id'=>$agent_id));
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('status'=>2),array('mid'=>$agent_id));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('status'=>1),array('mid'=>$agent_id));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($zhan_id){
		$member=$this->get('member',array('id'=>$zhan_id));
		if($member['stop']){
			return;
		}
		$level=pdo_getcolumn('jy_mzhan_level',array('id'=>$member['level_id']),'level');
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=true;
			foreach($level_up_types as $type){
				//file_put_contents(ROOT_D.'/2.txt',$type."\n",FILE_APPEND);
				if(!$this->level_up_types[$type]){
					continue;
				}
				if($type=='buy_goods' && $lv['goods_ids']){
						$goods_ids=json_decode($lv['goods_ids'],true);
						foreach($goods_ids as $gv){
								if($type=='buy_goods'){
									$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$member['mid'],'goodsid'=>$gv['id']),'total');
									if($isbuynum>0){
										break;
									}
								}
						}
						if(!$isbuynum){
							$up=false;
							break;
						}
				}elseif($type=='self_order_price'){
					$self_order_price=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$member['mid']),'price');
					//file_put_contents(ROOT_D.'/1.txt',$type.'~'.$self_order_price);
					if($self_order_price<$conditions[$type]){
						$up=false;
						break;
					}
				}elseif($type=='self_order_num'){
					$self_order_num=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$member['mid']),'id');
					//file_put_contents(ROOT_D.'/1.txt',$type.'~'.$self_order_num);
					if($self_order_num<$conditions[$type]){
						$up=false;
						break;
					}
				}else{
					if($member[$type]<$conditions[$type]){
						$up=false;
						break;
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('member',$update,array('id'=>$zhan_id));
		}
		return true;
	}
	/*
		邀请奖励
	*/
	public function upyao_fee($merchid=0){
		$user=pdo_get('jy_merch_user',array('id'=>$merchid));
		$zhan_id=$user['zhan_id'];
		if(!$zhan_id){
			return false;
		}
		$zhaoshang=pdo_get('jy_mzhan_member',array('id'=>$zhan_id));
		$mid=$zhaoshang['mid'];
		$mid=$mid?:$_SESSION['uid'];
		$set=$this->plugin_setting('zhaoshang');
		if($set['yao_open'] && $mid){
			foreach($set['yao_set'] as $k=>$yv){
				if($zhaoshang['level_id']!=$yv['level_id']){
					continue;
				}
				//$num=$this->pdo_count('user',array('zhan_id'=>$zhan_id,'is_yajinfee >'=>0,'uniacid'=>$_SESSION['uniacid']));
				if($yv['fee']>0){
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mid,
						'zhan_id'=>$zhan_id,
						'area_code'=>$zhaoshang['code'],
						'level'=>$zhaoshang['level_id'],
						'money'=>$yv['fee'],
						'commission_type'=>$commissioninfo['commissiontype']?:$set['commissiontype'],
						'createtime'=>TIMESTAMP,
						'yao_key'=>$k,
						'type'=>1,
						'status'=>1,
					);
					$this->insert('commission',$new_commission);
				}
			}
		}
		return false;
	}
}